package com.agileboot.admin.controller.performance;

import com.agileboot.common.core.dto.ResponseDTO;
import com.agileboot.common.core.page.PageDTO;
import com.agileboot.domain.performance.teaching.TeachingWorkloadApplicationService;
import com.agileboot.domain.performance.teaching.command.AddTeachingWorkloadCommand;
import com.agileboot.domain.performance.teaching.command.UpdateTeachingWorkloadCommand;
import com.agileboot.domain.performance.teaching.dto.TeachingWorkloadDTO;
import com.agileboot.domain.performance.teaching.dto.TeachingWorkloadImportResultDTO;
import com.agileboot.domain.performance.teaching.dto.TeachingWorkloadImportTemplateDTO;
import com.agileboot.domain.performance.teaching.query.TeachingWorkloadQuery;
import com.agileboot.common.utils.poi.CustomExcelUtil;
import com.agileboot.infrastructure.user.AuthenticationUtils;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 教学工作量控制器
 */
@Tag(name = "教学工作量API", description = "教学工作量相关的增删查改")
@RestController
@RequestMapping("/performance/teaching")
@RequiredArgsConstructor
public class TeachingWorkloadController {

    private final TeachingWorkloadApplicationService teachingWorkloadApplicationService;

    @Operation(summary = "获取教学工作量列表")
    @GetMapping("/list")
    public ResponseDTO<PageDTO<TeachingWorkloadDTO>> list(TeachingWorkloadQuery query) {
        PageDTO<TeachingWorkloadDTO> pageDTO = teachingWorkloadApplicationService.getTeachingWorkloadList(query);
        return ResponseDTO.ok(pageDTO);
    }

    @Operation(summary = "获取教学工作量详情")
    @GetMapping("/{id}")
    public ResponseDTO<TeachingWorkloadDTO> getInfo(@PathVariable Long id) {
        TeachingWorkloadDTO dto = teachingWorkloadApplicationService.getTeachingWorkloadInfo(id);
        return ResponseDTO.ok(dto);
    }

    @Operation(summary = "新增教学工作量")
    @PostMapping
    public ResponseDTO<Void> add(@RequestBody AddTeachingWorkloadCommand addCommand) {
        teachingWorkloadApplicationService.addTeachingWorkload(addCommand);
        return ResponseDTO.ok();
    }

    @Operation(summary = "修改教学工作量")
    @PutMapping
    public ResponseDTO<Void> edit(@RequestBody UpdateTeachingWorkloadCommand updateCommand) {
        teachingWorkloadApplicationService.updateTeachingWorkload(updateCommand);
        return ResponseDTO.ok();
    }

    @Operation(summary = "删除教学工作量")
    @DeleteMapping
    public ResponseDTO<Void> delete(@RequestBody List<Long> ids) {
        teachingWorkloadApplicationService.deleteTeachingWorkload(ids);
        return ResponseDTO.ok();
    }

    /**
     * 导出所有教学工作量
     * @param response HttpServletResponse
     * @param query 查询条件
     */
    @Operation(summary = "导出教学工作量")
    @GetMapping("/excel")
    public void export(HttpServletResponse response, TeachingWorkloadQuery query) {
        List<TeachingWorkloadDTO> all = teachingWorkloadApplicationService.getTeachingWorkloadListAll(query);
        CustomExcelUtil.writeToResponse(all, TeachingWorkloadDTO.class, response);
    }

    /**
     * 下载导入模板
     * @param response HttpServletResponse
     */
    @Operation(summary = "下载教学工作量导入模板")
    @GetMapping("/excelTemplate")
    public void downloadTemplate(HttpServletResponse response) {
        List<TeachingWorkloadImportTemplateDTO> templateData = teachingWorkloadApplicationService.getImportTemplate();
        CustomExcelUtil.writeToResponse(templateData, TeachingWorkloadImportTemplateDTO.class, response);
    }

    /**
     * 导入教学工作量
     * @param file Excel文件
     * @return 导入结果
     */
    @Operation(summary = "导入教学工作量")
    @PostMapping("/excel")
    public ResponseDTO<TeachingWorkloadImportResultDTO> importExcel(@RequestParam("file") MultipartFile file) {
        // 读取Excel文件
        List<TeachingWorkloadImportTemplateDTO> importDataList = 
                CustomExcelUtil.readFromRequest(TeachingWorkloadImportTemplateDTO.class, file);
        
        // 获取当前登录用户ID
        Long importedByUserId = AuthenticationUtils.getUserId();
        
        // 执行批量导入
        TeachingWorkloadImportResultDTO result = teachingWorkloadApplicationService.batchImport(importDataList, importedByUserId);
        
        return ResponseDTO.ok(result);
    }
}
